package com.yungu.swift.api.route.v1.driver;

import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.facade.DriverFacade;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.vo.ApiDriverOrderVo;
import com.yungu.swift.order.model.vo.JsonOrderDetailVo;
import com.yungu.swift.order.model.vo.JsonOrderListVo;
import com.yungu.swift.order.model.vo.OrderEstimateVo;
import com.yungu.swift.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

/**
 * 描述：
 *
 * @author Zerrion
 * @Time 2016/12/29
 */
@Controller
@RequestMapping(value = "/api/v1/driver")
@Api(value = "【司机】【订单】【专车】订单接口", tags = "专车司机订单相关操作")
@Slf4j
public class DriSpecialOrderApi extends BaseApi {

    @Autowired
    private DriverFacade driverFacade;

    @RequestMapping(value = {"/token/specialOrder/grab", "/token/ExpressOrder/grab", "/token/order/grab"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机抢单接口", notes = "抢单", response = ResponseData.class)
    public ResponseData<JsonOrderDetailVo> grab(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "token", value = "token", required = true) @RequestParam("token") String token,
            @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText,
            HttpServletRequest request) {
        return driverFacade.grab(getUserUid(request), orderUuid, uploadText);
    }

    @RequestMapping(value = {"/token/specialOrder/pickUpPas", "/token/ExpressOrder/pickUpPas", "/token/order/pickUpPas"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机去接乘客接口", notes = "去接乘客", response = ResponseData.class)
    public ResponseData<Boolean> pickUpPas(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText,
            HttpServletRequest request) {
        return driverFacade.pickUpPas(getUserUid(request), orderUuid, uploadText);
    }

    @RequestMapping(value = {"/token/specialOrder/depart", "/token/ExpressOrder/depart", "/token/order/depart"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机到达上车地点接口", notes = "到达上车地点", response = ResponseData.class)
    public ResponseData<Boolean> depart(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "token", value = "token", required = true) @RequestParam("token") String token,
            @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText,
            HttpServletRequest request) {
        return driverFacade.depart(getUserUid(request), orderUuid, uploadText);
    }

    @RequestMapping(value = {"/token/specialOrder/geton", "/token/ExpressOrder/geton", "/token/order/geton"}
            , method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机接到乘客接口", notes = "乘客已上车", response = ResponseData.class)
    public ResponseData<Boolean> geton(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "token", value = "token", required = true) @RequestParam("token") String token,
            @ApiParam(name = "waitMile", value = "空驶里程（司机出发到接到乘客）", required = false) @RequestParam(required = false) Double waitMile,
            @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText,
            HttpServletRequest request) {
        return driverFacade.geton(getUserUid(request), orderUuid, waitMile, uploadText);
    }

    @RequestMapping(value = {"/token/specialOrder/arrive"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机到达目的地接口", notes = "到达目的地", response = ResponseData.class)
    public ResponseData<Boolean> arrive(
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "tripDistance", value = "实际行驶里程(km)", required = true) @RequestParam Double tripDistance,
            @ApiParam(name = "destAddress", value = "终点地址标题") @RequestParam(required = false) String destAddress,
            @ApiParam(name = "destDetailAddress", value = "终点的详细地址") @RequestParam(required = false) String destDetailAddress,
            @ApiParam(name = "destCity", value = "终点城市") @RequestParam(required = false) String destCity,
            @ApiParam(name = "destLng", value = "终点经度") @RequestParam(required = false) Double destLng,
            @ApiParam(name = "destLat", value = "终点纬度") @RequestParam(required = false) Double destLat,
            @ApiParam(name = "uploadText", value = "长连接上传的报文") @RequestParam(required = false) String uploadText,
            @ApiParam(name = "tripRecording", value = "行程录音") @RequestParam(required = false) String tripRecording,
            HttpServletRequest request) {
        return driverFacade.arrive(getUserUid(request), orderUuid, tripDistance, CommonConstant.BUSINESS_TYPE_SPEC,
                destAddress, destDetailAddress, destCity, destLng, destLat, uploadText, tripRecording);
    }

    @RequestMapping(value = {"/token/specialOrder/confirmFare", "/token/ExpressOrder/confirmFare"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机确认费用接口", notes = "确认费用", response = ResponseData.class)
    public ResponseData<Boolean> confirmFare(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "highwayFare", value = "高速费") @RequestParam(required = false) Integer highwayFare,
            @ApiParam(name = "roadBridgeFare", value = "路桥费") @RequestParam(required = false) Integer roadBridgeFare,
            @ApiParam(name = "otherFare", value = "其他费") @RequestParam(required = false) Integer otherFare,
            @ApiParam(name = "uploadText", value = "长连接上传的报文", required = false) @RequestParam(required = false) String uploadText,
            HttpServletRequest request) {
        return driverFacade.confirmFare(getUserUid(request), orderUuid, highwayFare, roadBridgeFare, otherFare, uploadText);
    }

    @RequestMapping(value = {"/token/specialOrder/status"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机首页获取订单状态", notes = "订单状态", response = ResponseData.class)
    public ResponseData<Map<String, Object>> status(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        return driverFacade.status(getUserUid(request), CommonConstant.BUSINESS_TYPE_SPEC);
    }

    @RequestMapping(value = {"/token/specialOrder/list", "/token/ExpressOrder/list", "/token/order/list"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机获取订单列表接口", notes = "", response = ResponseData.class)
    public ResponseData<PageList<JsonOrderListVo>> list(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "当前页") @RequestParam(value = "nowPage", required = false) Integer nowPage,
            HttpServletRequest request) {
        return driverFacade.list(getUserUid(request), nowPage);
    }

    @RequestMapping(value = {"/token/order/listNew"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车司机获取订单列表接口", notes = "", response = ResponseData.class)
    public ResponseData<ApiDriverOrderVo> driverOrderlist(
            @ApiParam(name = "nowPage", value = "当前页") @RequestParam(value = "nowPage", required = false) Integer nowPage,
            @ApiParam(name = "type", value = "tab类型默认全部，0：全部 1：本日 2：本周 3：本月") @RequestParam(value = "type", defaultValue = "0") Integer type,
            @ApiParam(name = "businessType", value = "业务类型") @RequestParam(value = "businessType") Integer businessType,
            @ApiParam(name = "startTime", value = "开始时间") @RequestParam(value = "startTime", required = false) String startTime,
            @ApiParam(name = "endTime", value = "结束时间") @RequestParam(value = "endTime", required = false) String endTime,
            HttpServletRequest request) {
        String driUuid = getUserUid(request);
        if (StringUtils.isEmpty(driUuid)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取不到司机信息");
        }
        Date startDate = null;
        Date endDate = null;
        try {
            if (StringUtils.isNotEmpty(startTime)) {
                startDate = DateUtils.parseDate(startTime, "yyyy-MM-dd");
            }
            if (StringUtils.isNotEmpty(endTime)) {
                endDate = DateUtils.parseDate(endTime, "yyyy-MM-dd");
            }
        } catch (ParseException e) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "时间格式错误，请检查");
        }
        ApiPageParam apiPageParam = new ApiPageParam(nowPage);
        return driverFacade.listNew(driUuid, type, businessType, startDate, endDate, apiPageParam);
    }


    @RequestMapping(value = {"/token/specialOrder/info", "/token/ExpressOrder/info", "/token/order/info"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车订单详情接口", notes = "订单详情", response = ResponseData.class)
    public ResponseData<JsonOrderDetailVo> info(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam(value = "orderUuid") String orderUuid,
            HttpServletRequest request) {
        ResponseData<JsonOrderDetailVo> info = driverFacade.info(getUserUid(request), orderUuid);
        return info;
    }

    @RequestMapping(value = {"/token/specialOrder/orderFare", "/token/ExpressOrder/orderFare"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "专车订单费用", notes = "专车订单费用")
    public ResponseData<Map<String, Object>> orderFare(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            HttpServletRequest request) {
        return driverFacade.orderFare(getUserUid(request), orderUuid, 2);
    }

    @RequestMapping(value = {"/token/specialOrder/getRealtimeFare", "/token/ExpressOrder/getRealtimeFare"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "获取订单的时时价格和时间", notes = "获取订单的时时价格和时间")
    public ResponseData<OrderEstimateVo> getRealtimeFare(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid) throws Exception {
        log.error("查询实时价格");
        return driverFacade.getRealtimeFare(appid, orderUuid);
    }
}
